import {app, BrowserWindow, ipcMain, Menu, Tray, dialog} from 'electron'
import path from "path";
import {trays} from './config'
const nodeAbi = require('node-abi');
const m = require('.//modual/logger');

import {
  initFaceEngine,
  detectFaceFromFile,
  detectFaceFromBase64,
  extractFeature,
  featureProcess,
  releaseEngine,
} from './face'

// console.log("process.versions.node",process.versions.node);
// console.log("process.versions.electron",process.versions.electron);
// const ver1 = nodeAbi.getAbi('11.1.0', 'electron');
// const ver2 = nodeAbi.getAbi('12.18.3', 'node');
// console.log("electron-abi",ver1);
// console.log("node-abi",ver2);

let appTray = null;
if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}

// 初始化接口
const engine = initFaceEngine();


let mainWindow;
const winURL = process.env.NODE_ENV === 'development' ? `http://localhost:9080` : `file://${__dirname}/index.html`;

function createWindow () {
  let isFrame = process.env.NODE_ENV === 'development';
  let isFullscreen = process.env.NODE_ENV !== 'development';

  mainWindow = new BrowserWindow({
    icon: path.join(trays, 'tray.ico'),
    title: "人脸识别Demo",
    fullscreenWindowTitle: true,
    height: 768,
    width: 1080,
    useContentSize: true,
    frame: isFrame,
    webPreferences:{
      nodeIntegration: true,
      webgl: true,
      plugins: true,
      enableRemoteModule:true
    },
    show: false,
    fullscreen: isFullscreen,
    skipTaskbar: false,
  });


  // 设置主窗口全局Id
  global.mainId = mainWindow.id;
  mainWindow.loadURL(winURL);

  // 设置托盘图标
  setAppTray();

  mainWindow.on('ready-to-show', function () {
    // mainWindow.maximize();
    mainWindow.show() // 初始化后再显示
  });

  mainWindow.on('closed', () => {
    releaseEngine(engine);
    mainWindow = null
  })
}

// 主线程 接收人脸数据
ipcMain.on('detect', (e, data) =>{

  if (!data.img){
    m.logger.warn("no data.img is received");
    return
  }
  // 将base64转为图片格式，调用虹软并返回
  let img = data.img.replace("data:image/png;base64,","").replace("'",'');
  let faceInfo = detectFaceFromBase64(engine,img);

  let singles,feature;
  // if ((faceInfo.multi && faceInfo.multi.faceNum > 0)){
    // feature = extractFeature(engine,faceInfo.data,faceInfo.multi.faceRect,faceInfo.multi.faceOrient);
    // singles = featureProcess(engine,faceInfo.data, null,4,false, true);
  // }

  // 向渲染 发送人脸信息
   mainWindow.webContents.send('faceInfos', {
     faceInfo: faceInfo,
     img:data.img,
     // feature:feature,
     // process:singles
   });

});

app.on('ready', createWindow);

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
});

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow()
  }
});

// 托盘图标
const setAppTray = () => {
  // 系统托盘右键菜单
  let trayMenuTemplate = [
    {
      label: '退出',
      click: function() {
        //ipc.send('close-main-window');

        app.quit()
      }
    }
  ];

  // 系统托盘图标目录
  appTray = new Tray(path.join(trays, 'tray.ico'));

  // 图标的上下文菜单
  const contextMenu = Menu.buildFromTemplate(trayMenuTemplate);

  // 设置此托盘图标的悬停提示内容
  appTray.setToolTip('人脸识别Demo');

  // 设置此图标的上下文菜单
  appTray.setContextMenu(contextMenu);

  if(mainWindow != null){
    appTray.on('click', () => {
      mainWindow.isVisible() ? mainWindow.hide() : mainWindow.show();
      mainWindow.isVisible() ? mainWindow.setSkipTaskbar(false) : mainWindow.setSkipTaskbar(true);
    });
  }

};
